// 纯Node.js的Pinia功能验证脚本
// 不依赖任何测试框架
import { setActivePinia, createPinia } from 'pinia'
import { defineStore } from 'pinia'

// 创建模拟的浏览器环境
const mockWindow = {
  localStorage: {
    data: new Map<string, string>(),
    getItem(key: string) {
      return this.data.get(key) || null
    },
    setItem(key: string, value: string) {
      this.data.set(key, value)
    },
    removeItem(key: string) {
      this.data.delete(key)
    },
    clear() {
      this.data.clear()
    },
  },
}

// 注入到全局对象
Object.defineProperty(global, 'window', {
  value: mockWindow,
  writable: true,
})

console.log('===== Pinia 功能验证开始 =====')
console.log(`Node.js 版本: ${process.version}`)
console.log(
  `Pinia 导入检查: defineStore类型=${typeof defineStore}, createPinia类型=${typeof createPinia}`
)

try {
  // 定义类型
  interface UserInfo {
    id: number
    username: string
    nickname: string
    avatar: string
  }

  interface UserState {
    userInfo: UserInfo
    token: string | null
    isLogin: boolean
  }

  // 创建store定义
  console.log('\n定义store...')
  const useUserStore = defineStore('user', {
    state: (): UserState => ({
      userInfo: { id: 0, username: '', nickname: '', avatar: '' },
      token: null,
      isLogin: false,
    }),
    actions: {
      async loginAction(data: { username: string; password: string; captcha: string }) {
        console.log('执行loginAction:', data)
        this.token = 'test-token-' + Date.now()
        this.userInfo = {
          id: 1,
          username: data.username,
          nickname: '测试用户',
          avatar: 'test-avatar.png',
        }
        this.isLogin = true
        window.localStorage.setItem('token', this.token)
        return { code: 0, msg: 'success' }
      },
      logoutAction() {
        console.log('执行logoutAction')
        this.token = null
        this.userInfo = { id: 0, username: '', nickname: '', avatar: '' }
        this.isLogin = false
        window.localStorage.removeItem('token')
      },
    },
    getters: {
      isLoggedIn(): boolean {
        return this.isLogin && !!this.token
      },
    },
  })

  console.log(`store定义结果: useUserStore类型=${typeof useUserStore}`)

  // 验证useUserStore是否为函数
  if (typeof useUserStore !== 'function') {
    throw new Error(`useUserStore不是函数，而是${typeof useUserStore}`)
  }

  // 激活Pinia
  console.log('\n激活Pinia...')
  const pinia = createPinia()
  setActivePinia(pinia)
  console.log('Pinia已激活')

  // 创建store实例
  console.log('\n创建store实例...')
  const store = useUserStore()
  console.log(`store实例创建成功: store类型=${typeof store}`)

  // 验证store实例
  if (typeof store !== 'object') {
    throw new Error(`store实例不是对象，而是${typeof store}`)
  }

  // 测试初始状态
  console.log('\n测试初始状态:')
  console.log(`- isLogin: ${store.isLogin}`)
  console.log(`- token: ${store.token}`)
  console.log(`- username: ${store.userInfo.username}`)
  console.log(`- isLoggedIn getter: ${store.isLoggedIn}`)

  // 测试登录功能
  console.log('\n测试登录功能...')
  await store.loginAction({
    username: 'testuser',
    password: 'password123',
    captcha: '123456',
  })

  console.log('登录后状态:')
  console.log(`- isLogin: ${store.isLogin}`)
  console.log(`- token: ${store.token}`)
  console.log(`- username: ${store.userInfo.username}`)
  console.log(`- isLoggedIn getter: ${store.isLoggedIn}`)
  console.log(`- localStorage token: ${window.localStorage.getItem('token')}`)

  // 测试登出功能
  console.log('\n测试登出功能...')
  store.logoutAction()

  console.log('登出后状态:')
  console.log(`- isLogin: ${store.isLogin}`)
  console.log(`- token: ${store.token}`)
  console.log(`- username: ${store.userInfo.username}`)
  console.log(`- isLoggedIn getter: ${store.isLoggedIn}`)
  console.log(`- localStorage token: ${window.localStorage.getItem('token')}`)

  // 测试创建多个实例
  console.log('\n测试创建多个store实例...')
  const store2 = useUserStore()
  console.log(`store2与store是否为同一实例: ${store === store2}`)

  console.log('\n===== Pinia 功能验证成功 =====')
} catch (error) {
  console.error('\n===== Pinia 功能验证失败 =====')
  console.error('错误:', error)
  process.exit(1)
}
